package com.example.shop.controller;

import com.alibaba.fastjson.JSON;
import com.example.shop.model.OrderForm;
import com.example.shop.model.User;
import com.example.shop.service.OrderService;
import com.example.shop.utils.PageBean;
import com.example.shop.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderFromDataController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/addOrder")
    public String addOrder(PageBean<OrderForm> pageBean, String token) {
        int userId = TokenUtil.getUserId(token);
        System.err.println(pageBean);
        System.err.println(userId);
        pageBean = orderService.getPageBean(userId, pageBean.getPageNo(), pageBean.getPageSize());
        Map<String, Object> map = new HashMap<>();
        map.put("pageBean", pageBean);
        return JSON.toJSONString(map);
    }

    @PostMapping("/deleteOrder")
    public Map<String, String> deleteOrder(@RequestBody int id) {
        System.err.println(id);
        Map<String, String> map = new HashMap<>();
        int count = orderService.deleteById(id);
        if (count > 0) {
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return map;
    }
}
